package com.gw.vip.server.service.dao_service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gw.vip.common.constants.VipConstants;
import com.gw.vip.common.enums.StatusEnums;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.common.util.Snowflake;
import com.gw.vip.server.dao.BsOrderMapper;
import com.gw.vip.server.dto.ChargeDto;
import com.gw.vip.server.dto.RefundDto;
import com.gw.vip.server.dto.SaleDto;
import com.gw.vip.server.enums.TxnTypeEnums;
import com.gw.vip.server.po.BsOrder;
import com.gw.vip.server.po.HisOrder;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author GW
 * @description
 * @since 2022-04-28
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BsOrderService extends ServiceImpl<BsOrderMapper, BsOrder> {

    private final Snowflake snowflake;
    private final HisOrderService hisOrderService;

    /**
     * 订单是否已经存在
     * @param shopId
     * @param clientOrder
     * @return
     */
    public boolean isExist(String shopId, String clientOrder) {
        BsOrder bsOrder = this.baseMapper.getByBase(shopId, clientOrder);
        if (bsOrder !=null){
            return true;
        }

        HisOrder hisOrder = hisOrderService.getByBase(shopId, clientOrder);
        if (hisOrder != null){
            return true;
        }

        return false;
    }

    /**
     * 充值构建
     * @param dto
     * @return
     */
    public BsOrder initByCharge(ChargeDto dto) {
        BsOrder bsOrder = new BsOrder();
        bsOrder.setId(snowflake.nextStringId());
        bsOrder.setClientOrder(dto.getClientOrder());
        bsOrder.setShopId(dto.getShopId());
        bsOrder.setUserId(dto.getUserId());
        bsOrder.setOrderType(TxnTypeEnums.CHARGE.getKey());
        bsOrder.setTxnAmt(dto.getPayAmt().add(dto.getRebateAmt()));
        bsOrder.setPayAmt(dto.getPayAmt());
        bsOrder.setRebateAmt(dto.getRebateAmt());
        bsOrder.setRefundAmt(BigDecimal.ZERO);
        bsOrder.setStatus(StatusEnums.TRANSACTION.getCode());
        bsOrder.setCompleteTime(LocalDateTime.now());
        bsOrder.setCrtTime(bsOrder.getCompleteTime());
        bsOrder.setCrtName(VipConstants.SYS);
        bsOrder.setUpdTime(bsOrder.getCrtTime());
        bsOrder.setUpdName(VipConstants.SYS);
        return bsOrder;
    }

    /**
     * 消费构建
     * @param dto
     * @return
     */
    public BsOrder initTransFromSale(SaleDto dto) {
        BsOrder bsOrder = new BsOrder();
        bsOrder.setId(snowflake.nextStringId());
        bsOrder.setClientOrder(dto.getClientOrder());
        bsOrder.setShopId(dto.getShopId());
        bsOrder.setUserId(dto.getUserId());
        bsOrder.setOrderType(TxnTypeEnums.SALE.getKey());
        bsOrder.setTxnAmt(dto.getTxnAmt());
        bsOrder.setPayAmt(BigDecimal.ZERO);
        bsOrder.setRebateAmt(BigDecimal.ZERO);
        bsOrder.setRefundAmt(BigDecimal.ZERO);
        bsOrder.setStatus(StatusEnums.TRANSACTION.getCode());
        bsOrder.setCompleteTime(LocalDateTime.now());
        bsOrder.setCrtTime(bsOrder.getCompleteTime());
        bsOrder.setCrtName(VipConstants.SYS);
        bsOrder.setUpdTime(bsOrder.getCrtTime());
        bsOrder.setUpdName(VipConstants.SYS);
        return bsOrder;
    }

    public Integer getInOriCount(LocalDateTime clearMaxTime) {
        return this.baseMapper.getInOriCount(clearMaxTime);
    }

    public void setClearDate(LocalDate clearDate, LocalDateTime clearMaxTime) {
        this.baseMapper.setClearDate(clearDate, clearMaxTime);
    }

    public List<BsOrder> lockListByClearDate(LocalDate clearDate) {
        return this.baseMapper.lockListByClearDate(clearDate);
    }

    public void deleteById(String id) {
        this.baseMapper.deleteById(id);
    }

    public BsOrder initTransFromRefund(BsOrder srcBsOrder, RefundDto dto) {
        BsOrder bsOrder = new BsOrder();
        bsOrder.setId(snowflake.nextStringId());
        bsOrder.setClientOrder(dto.getClientOrder());
        bsOrder.setShopId(dto.getShopId());
        bsOrder.setUserId(srcBsOrder.getUserId());
        bsOrder.setOrderType(TxnTypeEnums.REFUND.getKey());
        bsOrder.setTxnAmt(dto.getTxnAmt());
        bsOrder.setPayAmt(BigDecimal.ZERO);
        bsOrder.setRebateAmt(BigDecimal.ZERO);
        bsOrder.setStatus(StatusEnums.TRANSACTION.getCode());
        bsOrder.setSrcId(srcBsOrder.getId());
        bsOrder.setRefundAmt(BigDecimal.ZERO);
        bsOrder.setCompleteTime(LocalDateTime.now());
        bsOrder.setCrtTime(bsOrder.getCompleteTime());
        bsOrder.setCrtName(VipConstants.SYS);
        bsOrder.setUpdTime(bsOrder.getCrtTime());
        bsOrder.setUpdName(VipConstants.SYS);
        return bsOrder;
    }

    public BsOrder getByBase(String shopId, String clientOrder) {
        if (StringUtils.isBlank(shopId)){
            log.error("shopId为空！");
            throw new ServiceException("shopId为空");
        }
        if (StringUtils.isBlank(clientOrder)){
            log.error("clientOrder为空");
            throw new ServiceException("clientOrder为空");
        }
        QueryWrapper<BsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BsOrder::getShopId, shopId);
        queryWrapper.lambda().eq(BsOrder::getClientOrder, clientOrder);
        return getOne(queryWrapper);
    }

    public BsOrder initTransFromAdminRefund(BsOrder srcBsOrder) {
        BsOrder bsOrder = new BsOrder();
        bsOrder.setId(snowflake.nextStringId());
        bsOrder.setClientOrder(snowflake.sysId());
        bsOrder.setShopId(srcBsOrder.getShopId());
        bsOrder.setUserId(srcBsOrder.getUserId());
        bsOrder.setOrderType(TxnTypeEnums.REFUND.getKey());
        bsOrder.setTxnAmt(srcBsOrder.getTxnAmt());
        bsOrder.setPayAmt(BigDecimal.ZERO);
        bsOrder.setRebateAmt(BigDecimal.ZERO);
        bsOrder.setStatus(StatusEnums.TRANSACTION.getCode());
        bsOrder.setSrcId(srcBsOrder.getId());
        bsOrder.setRefundAmt(BigDecimal.ZERO);
        bsOrder.setCrtTime(LocalDateTime.now());
        bsOrder.setCrtName(VipConstants.SYS);
        bsOrder.setUpdTime(bsOrder.getCrtTime());
        bsOrder.setUpdName(VipConstants.SYS);
        return bsOrder;
    }
}
